﻿using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logistics.Car
{
    public  class VehicleRepository: IVehicleRepository
    {
        private readonly LogisticsDbContext _db;

        public VehicleRepository(LogisticsDbContext db)
        {
            _db = db;
        }

        public async Task<int> Create(Vehicle m)
        {
            _db.Vehicle.Add(m);
            return _db.SaveChanges();
        }

        public async Task<int> Delete(int Id)
        {
            var list = _db.Vehicle.Find(Id);
            _db.Vehicle.Remove(list);
           
            return _db.SaveChanges();
        }
        /// <summary>
        /// 基础车辆反填
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Vehicle> GetCarryById(int Id)
        {
            var list = _db.Vehicle.Find(Id);
            return list;
        }
        /// <summary>
        /// 基础车辆修改
        /// </summary>
        /// <param name="ca"></param>
        /// <returns></returns>
        public async Task<int> UpdateVehicle(Vehicle ca)
        {
            var list = await _db.Vehicle.FindAsync(ca.Id);
            list = ca;
            _db.Entry(list).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _db.SaveChanges();
        }
        /// <summary>
        /// 显示车辆信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<Vehicle>> GetVehicle()
        {
            var list = _db.Vehicle.ToList();
            return list;
        }
    }
}
